package com.demo.mapper;

import com.demo.dto.LeaveDTO;
import com.demo.dto.LeavePageQueryDTO;
import com.demo.dto.StudentDTO;
import com.demo.dto.StudentPageQueryDTO;
import com.demo.pojo.Leave;
import com.demo.pojo.Student;
import com.demo.vo.LeaveVO;
import com.demo.vo.StudentVO;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface StudentMapper {

    @Insert("insert into sys_student (stuID,classID,stuName,sex,address,stuTel,contact,contactTel) " +
            "values (#{stuID},{select classID from sys_class where className=#{className},#{stuName},#{sex},#{address},#{stuTel},#{contact},#{contactTel});")
    void addStudent(Student student);

    @Delete("delete from sys_student where stuID=#{stuID}")
    int delete(String stuID);

    @Select("<script>" +
            "SELECT s.*, " +
            "c.className AS className " +
            "FROM sys_student s " +
            "LEFT JOIN sys_classes c ON s.classID = c.classID " +
            "WHERE 1=1 " +
            "<if test='stuID != null and stuID.trim() != \"\"'> " +
            "AND s.stuID = #{stuID} " +
            "</if> " +
            "<if test='classID != null and classID.trim() != \"\"'> " +
            "AND s.classID = #{classID} " +
            "</if> " +
            "ORDER BY s.stuID " +
            "</script>")
    Page<StudentVO> pageQuery(StudentPageQueryDTO studentPageQueryDTO);

    @Select("select * from sys_leave where leaveID=#{leaveID}")
    Leave selectByLeaveID(String leaveID);

    @Update("UPDATE sys_student SET classID = (SELECT classID FROM sys_classes WHERE className = #{className}), " +
            "stuName = #{stuName}, sex = #{sex}, address = #{address}, stuTel = #{stuTel}, " +
            "contact = #{contact}, contactTel = #{contactTel} WHERE stuID = #{stuID}")
    void update(StudentDTO studentDTO);


    @Select("select * from sys_student where classID=#{classID}")
    Student selectByClassID(String classID);
}
